package com.qf.controller;

import com.github.pagehelper.PageInfo;
import com.qf.pojo.User;
import com.qf.service.UserService;
import com.qf.vo.ActionResult;
import com.qf.vo.OrderItem;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.apache.ibatis.javassist.expr.Instanceof;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @author yj
 * @description
 * @date
 */

@RestController
public class UserController {
    @Autowired
    UserService userService;

    @GetMapping("/user")
    public User findone(HttpSession session){
        Object user_obj = session.getAttribute ("user");
        if(user_obj!=null && user_obj instanceof User){
            User user = (User) user_obj;
            return user;
        }
        return null;
    }
    @GetMapping("/users")
    public int login(HttpServletRequest req){
        String tel = req.getParameter("tel");
        String password = req.getParameter("password");
        User user = userService.login(tel);
        if(user!=null && user.getPassword().equals(password)){
            HttpSession session = req.getSession();
            session.setAttribute("user",user);
            if (user.getTel().equals("111111") && user.getPassword().equals("111111")) {
                return 111;
            }
            return 1;
        }else{
            return 0;
        }
    }

    @GetMapping("loginName")
    public String loginName(HttpSession session){
        Object user = session.getAttribute ("user");
        if(user!=null && user instanceof User){
            return ((User) user).getTel ();
        }
        return "guest";
    }

    //转向注册页面
    @PostMapping("/users")
    public  int register(HttpServletRequest req){
        String tel = req.getParameter("tel");
        String password = req.getParameter("password");
        String repassword = req.getParameter("repassword");
        String email = req.getParameter("email");
        String invitation = req.getParameter("invitation");
        invitation=invitation==null?"":invitation;
        User user =new User(0,tel,password,email,invitation);
        User user1 = userService.register(user);
        if(user!=null){
            return 1;
        }else {
            return -1;
        }
    }

    @GetMapping("/users2")
    public PageInfo<User> findAll(HttpServletRequest req, HttpServletResponse resp){
        String page = req.getParameter("page");
        int p=page==null?1:Integer.valueOf(page);
        PageInfo<User> alls = userService.findAll(p,3);
        return alls;
    }

    @PostMapping("/user2")
    public User addUser(@RequestBody User user){
        User user1 = userService.addUser(user);
        return user1;
    }

    @PutMapping("/user")
    public User updateUser(@RequestBody User user){
        User user1 = userService.updateUser(user);
        return user1;
    }

    @DeleteMapping("/user{id}")
    public boolean deleteUser(@PathVariable("id") int id){
        Boolean aBoolean = userService.deleteUser(id);
        return aBoolean;
    }

    @GetMapping("/info")
    public ActionResult updateTel(HttpServletRequest request,HttpSession session){
        User user=new User ();
        String tel = request.getParameter ("tel");
        String email = request.getParameter ("email");
        String newpassword = request.getParameter ("newpassword");
        String oldpassword = request.getParameter ("oldpassword");
        Object user_obj = session.getAttribute ("user");
        if(user_obj!=null && user_obj instanceof User){
            user = (User) user_obj;
        }
        if (tel!=null) {
            if (userService.login (tel)!=null){
                return new ActionResult (2,"改手机号已经注册过",null);
            }else{
                user.setTel (tel);
            }
        }else if (email!=null){
            //发送验证邮箱信息。。。
            return new ActionResult (3,"请前往邮箱验证",null);
        }else if(oldpassword!=null){
            if (!user.getPassword ().equals(oldpassword)){
                return new ActionResult (2,"原密码输入不正确",null);
            }else {
                user.setPassword (newpassword);
            }
        }
        userService.updateUser (user);
        return new ActionResult (1,"修改成功",null);
    }

    @RequestMapping(value = "/loginout")
    public void loginout(HttpServletRequest req, HttpServletResponse resp){
        HttpSession session = req.getSession();
        session.setAttribute("user",null);
        try {
            resp.sendRedirect("index.html");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }










}

